package Demo1.tree;/*
 *@ 代阳敲的专属代码
 **/

import java.util.LinkedList;
import java.util.Queue;

public class BinaryTree {
    static class Node{
        char data;
        Node left;
        Node right;
        public Node(char data){
            this.data = data;
        }

        public Node() {
        }
    }
    public Node CreateTree() {
        Node A = new Node('A');
        Node B = new Node('B');
        Node C = new Node('C');
        Node D = new Node('D');
        Node E = new Node('E');
        Node F = new Node('F');
        Node G = new Node('G');
        Node H = new Node('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    public void preOrder(Node root){
        if(root==null) return;
        System.out.print(root.data+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    // 中序遍历
    public void inOrder(Node root){
        if(root==null) return;
        inOrder(root.left);
        System.out.print(root.data+" ");
        inOrder(root.right);
    }
    // 后序遍历
    public void postOrder(Node root){
        if(root==null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.data+" ");
    }
    //顺序遍历
    // 获取树中节点的个数
     public int size;
    public void SizeNode(Node root){
        if (root==null) return ;
        size ++;
        SizeNode(root.left);
        SizeNode(root.right);
    }
    //递归方式
    public int SizeTree2(Node root){
        if (root==null) return 0;
        return SizeTree2(root.left)+SizeTree2(root.right)+1;
    }
    /**
     *
     * 非递归方式
     * @param root
     * @return 树中叶子节点的个数
     */
    public int count = 0;
    public void getLeafNodeCount(Node root) {
        if (root == null) return ;
        if (root.left == null && root.right == null)
            count++;
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }
    /**
     *
     * 递归方式
     * @param root
     * @return 树中叶子节点的个数
     */
    public int getLeafNodeCount2(Node root) {
        if (root == null) return 0;
        if (root.left == null && root.right == null) return 1;
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }
    /**
     *
     * 获取第k层节点的个数
     * @param root
     * @return
     */
    public int getKLevelNodeCount(Node root,int k){
        if(root==null) return 0;
        if(k==1) return 1;
        return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }

    /**
     *
     * 求二叉树的高度
     * @param root
     * @return 树的高度
     */
    public int getHeight(Node root) {
        if(root==null) return 0;
        int leftHegiht =getHeight(root.left);
        int rightHegiht =getHeight(root.right);
        return  Math.max(leftHegiht,rightHegiht)+1;
       // return 1+Math.max(getHeight(root.left),getHeight(root.right))+1;
    }

    /**
     * / 检测值为value的元素是否存在
     * @param root
     * @param val
     * @return
     */

    /**
     * 查找是否含有某个值
     * @param root
     * @param val
     * @return
     */
    public Node find(Node root, int val) {
        if (root == null) return null;
        if(root.data==val) {
            return root;
        }
        if(find(root.left,val)!=null) return find(root.left,val);
        if(find(root.right,val)!=null) return find(root.right,val);
        return null;
    }

    /**
     * //层序遍历
     * @param root
     */
    public  void levelOrder(Node root) {
        Queue<Node> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            Node cur=queue.poll();
            System.out.print(cur.data+" ");
            if(cur.left!=null) {
                queue.offer(cur.left);
            }
            if(cur.right!=null) {
                queue.offer(cur.right);
            }
        }
    }

    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(Node root) {
        Queue<Node> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            Node cur=queue.poll();
            if(cur==null) {
                break;
            } else {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }
        }
        while(!queue.isEmpty()) {
           Node cur=queue.peek();
           if(cur==null) {
               queue.poll();
           } else {
               return false;
           }

        }
        return true;
    }
}
